home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_11 / 9n11034a < prev    next >
Text File  |  1991-09-25  |  6KB  |  231 lines

  1. #include <iostream.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. #ifndef TRUE
  7. #define TRUE 1
  8. #endif
  9.  
  10. #ifndef FALSE
  11. #define FALSE 0
  12. #endif
  13.  
  14. class gcd_cls {   // base class
  15.          unsigned long u,v,r;
  16. public:
  17.          unsigned long gcd (unsigned long a,unsigned long b);
  18.  
  19. };
  20.  
  21. class fraction : private gcd_cls { //derived class with private access
  22.                                    //to gcd_cls function gcd()
  23.          long n,np;
  24. public:
  25.          fraction operator+(fraction d);  //overload +
  26.          fraction operator-(fraction d);  //overload -
  27.          fraction operator*(fraction d);  //overload *
  28.          fraction operator/(fraction d);  //overload /
  29.          fraction operator=(fraction d);  //overload =
  30.          char operator<(fraction &d);  //overload less than
  31.          char operator>(fraction &d); //overload greater than
  32.          char operator==(fraction &d); //overload ==
  33.          char operator!=(fraction &d); //overload !=
  34.          void swap (fraction &a,fraction &b);
  35.          friend ostream &operator<<(ostream &stream, fraction &x);
  36.          friend istream &operator>>(istream &stream, fraction &x);
  37. };
  38.  
  39. unsigned long gcd_cls :: gcd (unsigned long a,unsigned long b)
  40. {
  41.          ldiv_t r;   //use special div functions for longs
  42.                      //requires correct type structure
  43.  
  44.          u=labs(a);  //make sure argument is a positive number
  45.          v=labs(b);  //ditto second argument
  46.          if (v==0)
  47.                  return u;
  48.          while (v!=0) {
  49.                  r=ldiv(u,v);
  50.                  u=v;
  51.                  v=r.rem;
  52.          }
  53.          return u;
  54. }
  55.  
  56. ostream &operator<<(ostream &stream, fraction &x)
  57. {
  58.         if (x.n==0 && x.np==1) // Trap 0/1 as zero
  59.                 stream << "Zero" ;
  60.         else {
  61.                 stream << x.n;
  62.                 stream << '/';
  63.                 stream << x.np;
  64.         }
  65.         return stream;
  66. }
  67.  
  68. istream &operator>>(istream &stream, fraction &x)
  69. {
  70.         int col,col2,row;
  71.  
  72.         col=wherex();  //get col current location
  73.         row=wherey();  //get current row
  74.  
  75.         gotoxy (col+5,row); //go foward 5 spaces, print slash
  76.         cout << '/';
  77.         col2=wherex();
  78.         gotoxy (col,row);  //go back
  79.         stream >> x.n;     // get numerator
  80.         gotoxy (col2,row-1); // go right of slash
  81.         stream >>x.np;          //get denominator
  82.         return stream;
  83. }
  84.  
  85. fraction fraction :: operator=(fraction x) //overload =
  86. {
  87.          n=x.n;
  88.          np=x.np;
  89.          return (*this);
  90. }
  91.  
  92. char fraction :: operator<(fraction &x) // overload less than
  93. {
  94.         fraction temp;
  95.  
  96.         temp=*this-x;
  97.         if (temp.n<0)
  98.                 return TRUE;
  99.         else
  100.                 return FALSE;
  101.  
  102. }
  103.  
  104. char fraction :: operator>(fraction &x) //overload greater than
  105. {
  106.         if ( !(*this<x || *this==x) )
  107.                 return TRUE;
  108.         else
  109.                 return FALSE;
  110. }
  111.  
  112.  
  113.  
  114. char fraction :: operator!=(fraction &x) //overload !=
  115. {
  116.         if ( (n!=x.n) || (np!=x.np))
  117.                 return TRUE;
  118.         else
  119.                 return FALSE;
  120. }
  121.  
  122. char fraction :: operator==(fraction &x) //overload ==
  123. {
  124.         if( (n==x.n) && (np==x.np))
  125.                 return TRUE;
  126.         else
  127.                 return FALSE;
  128. }
  129.  
  130. fraction fraction :: operator+(fraction x) // overload +
  131. {
  132.          fraction w;
  133.          long d1,t,d2;
  134.  
  135.          d1=gcd(np,x.np);
  136.          if (d1==1){
  137.                  w.n=(n*x.np)+(np*x.n);
  138.                  w.np=(np*x.np);
  139.                  return w;
  140.          }
  141.          if (d1>1) {
  142.                  t=n*(x.np/d1)+x.n*(np/d1);
  143.                  d2=gcd(t,d1);
  144.                  w.n=t/d2;
  145.                  w.np=(np/d1)*(x.np/d2);
  146.                  return w;
  147.          }
  148. }
  149.  
  150. fraction fraction :: operator-(fraction x) // over load -
  151. {
  152.          fraction w,tmpa,tmpb;
  153.          long d1,t,d2;
  154.  
  155.  
  156.          tmpa=*this;
  157.          tmpb=x;
  158.  
  159.          d1=gcd(tmpa.np,tmpb.np);
  160.          if (d1==1){
  161.                  w.n=(tmpa.n*tmpb.np)-(tmpa.np*tmpb.n);
  162.                  w.np=(tmpa.np*tmpb.np);
  163.                  return w;
  164.          }
  165.          if (d1>1) {
  166.                  t=n*(tmpb.np/d1)-tmpb.n*(tmpa.np/d1);
  167.                  d2=gcd(t,d1);
  168.                  w.n=t/d2;
  169.                  w.np=(tmpa.np/d1)*(tmpb.np/d2);
  170.                  return w;
  171.          }
  172. }
  173.  
  174.  
  175. fraction fraction :: operator*(fraction x) //overload *
  176. {
  177.          fraction temp;
  178.          long d;
  179.          d=gcd(n*x.n,np*x.np);
  180.          temp.n=n*x.n / d;
  181.          temp.np=np*x.np/d;
  182.          return temp;
  183. }
  184.  
  185. fraction fraction :: operator/(fraction x) //overload /
  186. {
  187.          fraction temp,tmp2;
  188.          // first we will invert
  189.          temp.n=x.np;
  190.          temp.np=x.n;
  191.          //now we will multiply
  192.          tmp2=temp* *this;
  193.          return tmp2;
  194. }
  195.  
  196.  
  197.  
  198. void fraction :: swap (fraction &a, fraction &b)
  199. {
  200.          fraction temp;
  201.                  temp=a;
  202.                  a=b;
  203.                  b=temp;
  204. }
  205.  
  206. void main(void)
  207. {
  208.          fraction a,b,c;
  209.          char ch;
  210.  
  211.          cout << "\nEnter fraction a: ";
  212.          cin >> a;
  213.          cout << "\nEnter fraction b: ";
  214.          cin >> b;
  215.          c=a+b;
  216.          cout << "\nAdd " << c;
  217.          c=a-b;
  218.          cout << "\nsub " << c;
  219.          c=a*b;
  220.          cout << "\nMult " << c;
  221.          c=a/b;
  222.          cout << "\nDiv " << c;
  223.          if (a<b) cout << "\n" << a << " is less than " << b;
  224.          if (b<a) cout << "\n" << b << " is less than " << a;
  225.          if (a>b) cout << "\n" << a << " is greater than " << b;
  226.          if (b>a) cout << "\n" << b << " is greater than " << a;
  227.          if (b==a) cout << "\nFractions are equal";
  228.          if (a!=b) cout << "\nFractions are not equal";
  229.  
  230. }
  231.